﻿using BCT.Models;
using BCT.Repositories.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCT.Repositories
{
    public class UserManageRepository : IUserManageRepository
    {
        //获取用户列表
        public List<UserModel> GetUserList(string username,int roleid)
        {
            using (var context = new EvaluateEntities())
            {
                if (string.IsNullOrEmpty(username) && roleid == 0)
                {
                    var userList = context.User
                               //.Where(s => s.UserPower == 1)
                               .Select(s => new UserModel()
                               {
                                   UserId = s.UserId,
                                   Email = s.Email,
                                   UserName = s.UserName,
                                   Role = s.Role.RoleName,
                                   RoleId = s.RoleId,
                                   Phone = s.Phone,
                                   //UserPower = s.UserPower,
                                   Active = s.Active
                               }).ToList();
                    return userList;
                }
                else if ((!string.IsNullOrEmpty(username)) && (!(roleid == 0))) {
                    var userList = context.User
                             .Where(/*s => s.UserPower == 1 &&*/ s => s.UserName.Contains(username) && s.RoleId == roleid)
                             .Select(s => new UserModel()
                             {
                                 UserId = s.UserId,
                                 Email = s.Email,
                                 UserName = s.UserName,
                                 Role = s.Role.RoleName,
                                 RoleId = s.RoleId,
                                 Phone = s.Phone,
                                // UserPower = s.UserPower
                             }).ToList();
                    return userList;
                }
                else
                {
                    var userList = context.User
                               .Where(/*s => s.UserPower == 1 &&*/ s => s.UserName.Contains(username) || s.RoleId == roleid)
                               .Select(s => new UserModel()
                               {
                                   UserId = s.UserId,
                                   Email = s.Email,
                                   UserName = s.UserName,
                                   Role = s.Role.RoleName,
                                   RoleId = s.RoleId,
                                   Phone = s.Phone,
                                   //UserPower = s.UserPower
                               }).ToList();
                    return userList;
                }
            }
        }

        //修改用户
        public string ModefyUser(UserModel model)
        {
            using (var context = new EvaluateEntities())
            {
                var ifhaveuser = context.User.FirstOrDefault(s => s.Email == model.Email && s.UserId != model.UserId);
                if(ifhaveuser != null)
                {
                    return "该邮箱已存在！";
                }
                else
                {
                    var user = context.User.FirstOrDefault(s => s.UserId == model.UserId);
                    user.UserName = model.UserName;
                   // user.UserPower = model.UserPower;
                    user.RoleId = model.RoleId;
                    user.Email = model.Email;
                    user.Phone = model.Phone;
                    if (context.SaveChanges() > 0)
                    {
                        return "修改成功！";
                    }
                    else
                    {
                        return "修改失败！";
                    }
                }             
            }
        }

        //添加用户（用户自己注册）
        public bool AddUser(UserModel model) {
            using (var context = new EvaluateEntities())
            {
                var user = context.User.FirstOrDefault(s => s.Email == model.Email);
                if (!string.IsNullOrEmpty(model.UserName) && user == null)
                {
                    context.User.Add(new User()
                    {
                        UserName = model.UserName,
                        RoleId = model.RoleId,
                        Email = model.Email,
                        Phone = model.Phone,
                        Password = model.Password,
                        //UserPower = model.UserPower
                    });
                    return context.SaveChanges() > 0;
                }
                return false;
            }
        }

        //禁用或启用用户
        public string LogDelete(UserModel model)
        {
            using (var context = new EvaluateEntities())

            {
                var user = context.User.FirstOrDefault(s => s.UserId == model.UserId);
                user.Active = model.Active;
                if (context.SaveChanges() > 0)
                {
                    return "修改成功！";
                }
                else
                {
                    return "修改失败！";
                }

            }
        }

        //获取数据库中所有的Power种类
        //public List<int?> GetPowerList()
        //{
        //    using (var context = new EvaluateEntities())
        //    {
        //        var powerList = context.User
        //                        .Select(s => s.UserPower).Distinct().ToList();
        //        return powerList;
        //    }
        //}

    }
}
